package cn.sdormitory.controller;

import cn.sdormitory.common.api.CommonPage;
import cn.sdormitory.common.api.CommonResult;
import cn.sdormitory.common.dto.ResultMsg;
import cn.sdormitory.common.utils.PropertiesUtils;
import cn.sdormitory.request.HttpRequest;
import cn.sdormitory.service.AppAttenceService;
import cn.sdormitory.service.AppUserService;
import cn.sdormitory.smartdor.vo.DormitoryAttenceVo;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @创建人：zhouyang
 * @创建时间：2020/12/29 14:36
 * @version：V1.0
 */
@RestController
@RequestMapping("/app/Index")
@Api("APP端首页")
public class AppIndexController {
    @Autowired
    private AppUserService appUserService;
    @Autowired
    private AppAttenceService appAttenceService;

    @ApiOperation("deviceOpen => APP首页开闸")
    @GetMapping(value = "/deviceOpen")
    public ResultMsg aiopen() {
        String ip = PropertiesUtils.get("device.properties","sdormitory.device1.ip");
        String key = PropertiesUtils.get("device.properties","sdormitory.device1.key");
        String url=ip+"/open";
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("key="+key);
        String result= HttpRequest.sendGet(url,stringBuffer.toString());
        JSONObject parseObject=JSONObject.parseObject(result);
        String status=parseObject.getString("status");
        if (""!=status && status!=null && status.equals("0")) {
            return ResultMsg.BY_SUCCESS("开闸成功!!!","");
        }else{
            return ResultMsg.BY_FAIL("开闸失败!!!");
        }
    }


    @ApiOperation("getRoleCount => APP首页获取当前用户是否拥有管理员或宿管老师角色")
    @GetMapping("/getRoleCount")
    public int getRoleCount(int id){
        int roleCount=appUserService.getRoleCount(id);
        return roleCount;
    }

    @ApiOperation("listDorAttence => APP首页查询宿舍考勤信息列表")
    @GetMapping(value = "/listDorAttence")
    public ResultMsg listDorAttence()  {
        List<DormitoryAttenceVo> dormitoryAttenceVoList=appAttenceService.getAppDorAttence();
        return ResultMsg.BY_SUCCESS("获取成功",dormitoryAttenceVoList);
    }


}
